package com.danskin.wechat.webapi;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.danskin.wechat.api.client.DanskinCRMApiClient;
import com.danskin.wechat.constant.DanskinConstants;
import com.danskin.wechat.dto.CRMMemberRequest;
import com.danskin.wechat.entity.CrmBrandInfo;
import com.danskin.wechat.entity.CustCouponRecord;
import com.danskin.wechat.entity.CustInfo;
import com.danskin.wechat.entity.CustOrderRecord;
import com.danskin.wechat.entity.WxMediaPlatform;
import com.danskin.wechat.model.AccessResponse;
import com.danskin.wechat.service.CouponService;
import com.danskin.wechat.service.CrmBrandInfoService;
import com.danskin.wechat.service.CustInfoService;
import com.danskin.wechat.service.CustOrderRecordService;
import com.danskin.wechat.service.WxFansInfoService;
import com.danskin.wechat.util.DateUtils;
import com.danskin.wechat.util.JsonUtils;
import com.danskin.wechat.util.LogUtil;
import com.danskin.wechat.util.Tools;
import com.danskin.wechat.util.VipLevelUtil;
import com.danskin.wechat.vo.CustInfoVo;

@RestController
@RequestMapping("api/cust")
public class CustomerInfoController extends BaseApiController {

	@Autowired
	private CustInfoService custInfoService;

	@Autowired
	private CouponService couponService;

	@Autowired
	private CustOrderRecordService orderRecordService;

	@Autowired
	private CrmBrandInfoService crmBrandInfoService;

	@Autowired
	private WxFansInfoService wxFansInfoService;

	/**
	 * 新人礼个人信息完善
	 * 
	 * @param appId
	 * @param info
	 * @return
	 */
	@RequestMapping(value = "/saveCustInfo/{appId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String saveCustInfo(@PathVariable("appId") String appId, @RequestBody Map<String, Object> info) {
		AccessResponse<CustInfo> access = new AccessResponse<CustInfo>();
		if (null != info) {
			if (info.containsKey("openId")) {
				String openId = (String) info.get("openId");
				info.remove("openId");
				if (info.containsKey("birthday")) {
					if ("" != info.get("birthday")) {
						Object date = DateUtils.toDate(Date.class, info.get("birthday"));
						info.put("birthday", date);
					} else {
						info.remove("birthday");
					}

				}
				CustInfo custInfo = Tools.toBean(info, CustInfo.class);
				CustInfo custInfo2 = custInfoService.updateCustInfoSelective(custInfo, openId);
				if (StringUtils.isNotBlank(String.valueOf(custInfo2))) {
					access.setErrorCode("0");
					access.setErrorMsg("成功");
				} else {
					access.setErrorCode("1");
					access.setErrorMsg("保存信息失败");
				}
			} else {
				access.setErrorCode("1");
				access.setErrorMsg("openId不能为空");
			}
		} else {
			access.setErrorCode("1");
			access.setErrorMsg("个人信息不能为空");
		}
		return JsonUtils.objectToJson(access);
	}

	/**
	 * 获取个人信息
	 * 
	 * @param wxOpenId
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/getCustInfo/{appId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String getCustInfo(@RequestBody Map<String, Object> map) throws Exception {
		AccessResponse<Map<String, Object>> access = new AccessResponse<Map<String, Object>>();
		if (map.containsKey("wxOpenId")) {
			Map<String, Object> map2 = custInfoService.queryByOpenId((String) map.get("wxOpenId"));
			if (null != map2) {
				if (map2.containsKey("vip")) {
					String vip = String.valueOf(map2.get("vip"));
					map2.put("vipLevel", VipLevelUtil.getVipLevelName(vip));
					map2.put("vip", vip.substring(1));
				}
				// if (map2.containsKey("subscribeTime")) {
				// long days =
				// DateUtils.getDistanceDays(String.valueOf(map2.get("subscribeTime")),
				// DateUtils.formatDate(new Date()));
				// if (days > 0) {
				// map2.put("timeDistance", days);
				// } else {
				// map2.put("timeDistance", 0);
				// }
				// } else {
				// map2.put("timeDistance", 0);
				// }
				if (map2.containsKey("cust_ex_id")) {
					CustCouponRecord record = new CustCouponRecord();
					record.setCustExId((Integer) map2.get("cust_ex_id"));
					record.setIsUsed(0);
					record.setExpiredEndTime(new Date());
					// 查询可用券
					int avaCoupon = couponService.selectAvaCouponByCondition(record);
					if (avaCoupon > 0) {
						map2.put("avaCoupon", avaCoupon);
					} else {
						map2.put("avaCoupon", 0);
					}

					// CustOrderRecord order = new CustOrderRecord();
					// order.setCustExId((Integer) map2.get("cust_ex_id"));
					// order.setTransYear(Integer.parseInt((new
					// SimpleDateFormat("yyyy").format(new Date()))));
					// Double amountByYear =
					// orderRecordService.selectPayAmountByYear(order);
					// if (amountByYear != null) {
					// map2.put("payAmount", amountByYear);
					// } else {
					// map2.put("payAmount", 0);
					// }

				}

				if (!map2.containsKey("nick_name")) {
					map2.put("nick_name", "");
				}
				if (!map2.containsKey("avaPoints")) {
					map2.put("avaPoints", 0);
				}
				if (!map2.containsKey("mobile")) {
					map2.put("mobile", "");
				}
				if (!map2.containsKey("gender")) {
					map2.put("gender", 0);
				}
				if (!map2.containsKey("birthday")) {
					map2.put("birthday", "");
				}
				if (!map2.containsKey("hobby")) {
					map2.put("hobby", "");
				}
				if (!map2.containsKey("province")) {
					map2.put("province", "");
				}
				if (!map2.containsKey("city")) {
					map2.put("city", "");
				}
				if (!map2.containsKey("district")) {
					map2.put("district", "");
				}
				if (!map2.containsKey("name")) {
					map2.put("name", "");
				}
				if (!map2.containsKey("vipLevel")) {
					map2.put("vipLevel", "");
				}
				if (!map2.containsKey("vip")) {
					map2.put("vip", "0");
				}
				access.setErrorCode("0");
				access.setErrorMsg("成功");
				access.setData(map2);
			} else {
				access.setErrorCode("1");
				access.setErrorMsg("获取信息失败");
			}

		} else {
			access.setErrorCode("1");
			access.setErrorMsg("wxOpenId不能为空");
		}
		return JsonUtils.objectToJson(access);
	}

	/**
	 * 查询会员基本信息
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryCustInfo/{appId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryCustInfo(@RequestBody Map<String, Object> map) throws Exception {
		AccessResponse<Map<String, Object>> access = new AccessResponse<Map<String, Object>>();
		if (map.containsKey("wxOpenId")) {
			Map<String, Object> map2 = custInfoService.queryByOpenId((String) map.get("wxOpenId"));
			if (null != map2) {
				if (map2.containsKey("vip")) {
					String vip = String.valueOf(map2.get("vip"));
					map2.put("vipLevel", VipLevelUtil.getVipLevelName(vip));
					map2.put("vip", vip.substring(1));
				}
				// if (map2.containsKey("subscribeTime")) {
				// long days =
				// DateUtils.getDistanceDays(String.valueOf(map2.get("subscribeTime")),
				// DateUtils.formatDate(new Date()));
				// if (days > 0) {
				// map2.put("timeDistance", days);
				// } else {
				// map2.put("timeDistance", 0);
				// }
				// } else {
				// map2.put("timeDistance", 0);
				// }
				if (!map2.containsKey("nick_name")) {
					map2.put("nick_name", "");
				}
				if (!map2.containsKey("avaPoints")) {
					map2.put("avaPoints", 0);
				}
				if (!map2.containsKey("mobile")) {
					map2.put("mobile", "");
				}
				if (!map2.containsKey("gender")) {
					map2.put("gender", 0);
				}
				if (!map2.containsKey("birthday")) {
					map2.put("birthday", "");
				}
				if (!map2.containsKey("hobby")) {
					map2.put("hobby", "");
				}
				if (!map2.containsKey("province")) {
					map2.put("province", "");
				}
				if (!map2.containsKey("city")) {
					map2.put("city", "");
				}
				if (!map2.containsKey("district")) {
					map2.put("district", "");
				}
				if (!map2.containsKey("name")) {
					map2.put("name", "");
				}
				if (!map2.containsKey("vipLevel")) {
					map2.put("vipLevel", "");
				}
				if (!map2.containsKey("vip")) {
					map2.put("vip", "0");
				}
				access.setErrorCode("0");
				access.setErrorMsg("成功");
				access.setData(map2);
			} else {
				access.setErrorCode("1");
				access.setErrorMsg("获取信息失败");
			}

		} else {
			access.setErrorCode("1");
			access.setErrorMsg("wxOpenId不能为空");
		}
		return JsonUtils.objectToJson(access);
	}

	/**
	 * 获取会员福利（等级、年消费金额）
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryCustBonus/{appId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryCustBonus(@RequestBody Map<String, Object> map) throws Exception {
		AccessResponse<Map<String, Object>> access = new AccessResponse<Map<String, Object>>();
		if (map.containsKey("wxOpenId")) {
			Map<String, Object> map2 = custInfoService.queryCustVipLevel((String) map.get("wxOpenId"));
			if (null != map2) {
				if (map2.containsKey("vip_level")) {
					String vip = String.valueOf(map2.get("vip_level"));
					map2.put("vipLevel", VipLevelUtil.getVipLevelName(vip));
					map2.put("vip", vip.substring(1));
				}
				if (!map2.containsKey("vipLevel")) {
					map2.put("vipLevel", "");
				}
				if (!map2.containsKey("vip")) {
					map2.put("vip", "0");
				}
				if (map2.containsKey("subscribe_time")) {
					long days = DateUtils.getDistanceDays(String.valueOf(map2.get("subscribe_time")),
							DateUtils.formatDate(new Date()));
					if (days > 0) {
						map2.put("timeDistance", days);
					} else {
						map2.put("timeDistance", 0);
					}
				} else {
					map2.put("timeDistance", 0);
				}
				CustOrderRecord order = new CustOrderRecord();
				order.setCustExId((Integer) map2.get("cust_ex_id"));
				order.setTransYear(Integer.parseInt((new SimpleDateFormat("yyyy").format(new Date()))));
				Double amountByYear = orderRecordService.selectPayAmountByYear(order);
				if (amountByYear != null) {
					map2.put("payAmount", amountByYear);
				} else {
					map2.put("payAmount", 0);
				}

				access.setErrorCode("0");
				access.setErrorMsg("成功");
				access.setData(map2);
			} else {
				access.setErrorCode("1");
				access.setErrorMsg("获取信息失败");
			}

		} else {
			access.setErrorCode("1");
			access.setErrorMsg("wxOpenId不能为空");
		}
		return JsonUtils.objectToJson(access);
	}

	/**
	 * 获取会员vip等级
	 * 
	 * @param map
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value = "/queryCustVipLevel/{appId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String queryCustVipLevel(@RequestBody Map<String, Object> map) throws Exception {
		AccessResponse<Map<String, Object>> access = new AccessResponse<Map<String, Object>>();
		if (map.containsKey("wxOpenId")) {
			Map<String, Object> map2 = custInfoService.queryCustVipLevel((String) map.get("wxOpenId"));
			if (null != map2) {
				if (map2.containsKey("vip_level")) {
					String vip = String.valueOf(map2.get("vip_level"));
					map2.put("vipLevel", VipLevelUtil.getVipLevelName(vip));
					map2.put("vip", vip.substring(1));
				}
				if (!map2.containsKey("vipLevel")) {
					map2.put("vipLevel", "");
				}
				if (!map2.containsKey("vip")) {
					map2.put("vip", "0");
				}

				access.setErrorCode("0");
				access.setErrorMsg("成功");
				access.setData(map2);
			} else {
				access.setErrorCode("1");
				access.setErrorMsg("获取信息失败");
			}

		} else {
			access.setErrorCode("1");
			access.setErrorMsg("wxOpenId不能为空");
		}
		return JsonUtils.objectToJson(access);
	}

	/**
	 * 会员注册（提供给李宁第三方的接口）
	 * 
	 * @param appId
	 * @param map
	 * @return
	 * @author makunsmac
	 */
	@RequestMapping(value = "/memberRegister/{appId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Object memberRegister(@PathVariable("appId") String appId, @RequestBody Map<String, Object> map) {
		LogUtil.info("=======会员注册参数：" + map.toString());
		// 校验必传参数
		if (StringUtils.isBlank(appId)) {
			return new AccessResponse<String>("-1", "公众号appId为空");
		}
		if (!isValid("openId", map)) {
			return new AccessResponse<String>("-1", "用户openId为空");
		}
		if (!isValid("mobile", map)) {
			return new AccessResponse<String>("-1", "电话号码为空");
		}
		if (!isValid("custName", map)) {
			return new AccessResponse<String>("-1", "用户姓名为空");
		}
		if (!isValid("birthday", map)) {
			return new AccessResponse<String>("-1", "用户生日为空");
		}
		if (!isValid("sex", map)) {
			return new AccessResponse<String>("-1", "用户性别为空");
		}

		// 获取accessToken和mpId
		WxMediaPlatform platForm = getMPWithAccessToken(appId);
		if (platForm == null) {
			return new AccessResponse<String>("-1", "appId错误");
		}

		// 根据mpId查询品牌
		CrmBrandInfo brandInfo = crmBrandInfoService.selectCrmBrandInfoByMpId(platForm.getId());
		if (brandInfo == null) {
			return new AccessResponse<String>("-1", "appId错误");
		}

		// 定义memberId和memberLevel
		String memberId, memberLevel;
		try {
			// 1.与CRM打交道
			// 封装新建会员对象用于同步给CRM
			CRMMemberRequest memberRequest = packageCRMMemberRequest(map, brandInfo);
			memberId = "";
			memberLevel = "";
			// 同步会员信息给CRM
			JSONObject memberJSON = DanskinCRMApiClient.createInstance().addMember(memberRequest);
			if (memberJSON != null && memberJSON.containsKey("status") && memberJSON.containsKey("member")) {
				memberId = memberJSON.getJSONObject("member").getString("memberId");
				memberLevel = memberJSON.getJSONObject("member").getString("memberLevel");
			} else {
				return new AccessResponse<String>("-1", "用户信息同步CRM异常");
			}
		} catch (Exception e) {
			return new AccessResponse<String>("-1", "用户注册异常");
		}

		// 注册
		return custInfoService.saveMemberRegister(map, platForm, memberId, memberLevel);

	}

	/**
	 * 积分变更（提供给李宁第三方的接口）
	 * 
	 * @param appId
	 * @param map
	 * @return
	 * @author makunsmac
	 */
	@RequestMapping(value = "/pointChange/{appId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Object pointChange(@PathVariable("appId") String appId, @RequestBody Map<String, Object> map) {

		LogUtil.info("=======积分变更参数：" + map.toString());
		if (StringUtils.isBlank(appId)) {
			return new AccessResponse<String>("-1", "公众号appId为空");
		}
		if (!isValid("openId", map)) {
			return new AccessResponse<String>("-1", "用户openId为空");
		}
		if (!isValid("addMinus", map)) {
			return new AccessResponse<String>("-1", "加减标志为空");
		}
		if (!isValid("integral", map)) {
			return new AccessResponse<String>("-1", "变更积分值为空");
		}
		if (!isValid("activityName", map)) {
			return new AccessResponse<String>("-1", "活动名称为空");
		}
		// 如果增减积分的标识不是加也不是减，返回错误信息
		if (!StringUtils.equals(map.get("addMinus").toString(), DanskinConstants.ADD_OR_MINUS_POINTS_ADD.toString())
				&& !StringUtils.equals(map.get("addMinus").toString(),
						DanskinConstants.ADD_OR_MINUS_POINTS_MINUS.toString())) {
			return new AccessResponse<String>("-1", "加减标志错误");
		}

		// 根据公众号查询品牌
		CrmBrandInfo brandInfo = crmBrandInfoService.selectCrmBrandInfoByAppId(appId);
		if (brandInfo == null) {
			return new AccessResponse<String>("-1", "无效的AppId");
		}

		// 根据用户微信openId获取用户信息
		 CustInfoVo custInfoDto =
		 custInfoService.queryCustInfoDtoByOpenId(map.get("openId").toString());
		// if (custInfoDto == null) {
		// return new AccessResponse<String>("-1", "该用户不存在");
		// }

		// 增减积分
		return custInfoService.pointChange(map, brandInfo);
	}

	/**
	 * 根据用户手机号或者用户openId判断会员身份（提供给李宁第三方的接口）
	 * 
	 * @param appId
	 * @param map
	 * @return
	 * @author makunsmac
	 */
	@RequestMapping(value = "/judgeUserIsMemberOrNot/{appId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Object judgeUserIsMemberOrNot(@PathVariable("appId") String appId, @RequestBody Map<String, Object> map) {
		LogUtil.info("=======会员身份判断参数：" + map.toString());

		try {
			// 数据校验
			if (StringUtils.isBlank(appId)) {
				return new AccessResponse<String>("-1", "公众号appId为空");
			}
			if (!isValid("openId", map) && !isValid("tel", map)) {
				return new AccessResponse<String>("-1", "用户openId和用户手机都为空");
			}

			// 获取mpId
			Integer mpId = getMpId(appId);
			// 优先判断手机号
			if (isValid("tel", map)) { // 如果传递的是用户手机号，根据手机号判断用户会员状态
				return custInfoService.judgeUserStatusByTel(map.get("tel").toString(), mpId);
			} else { // 如果传递的是用户openId，根据openId判断用户会员状态
				return custInfoService.judgeUserStatusByOpenId(map.get("openId").toString(), mpId);
			}
		} catch (Exception e) {
			LogUtil.error("=======会员身份判断异常", e);
			return new AccessResponse<String>("-1", "系统异常");
		}
	}

	/**
	 * 发送优惠券接口--现金券（提供给李宁第三方的接口）
	 * 
	 * @param appId
	 * @param map
	 * @return
	 * @author makunsmac
	 */
	// @RequestMapping(value = "/sendCoupon/{appId}", method =
	// RequestMethod.POST, produces = "application/json;charset=UTF-8")
	// public Object sendCoupon(@PathVariable("appId") String appId,
	// @RequestBody Map<String, Object> map) {
	// LogUtil.info("=======发送优惠券参数：" + map.toString());
	//
	// try {
	// // 数据校验
	// if (StringUtils.isBlank(appId)) {
	// return new AccessResponse<String>("-1", "公众号appId为空");
	// }
	// if (!isValid("openId", map) && !isValid("mobile", map)) {
	// return new AccessResponse<String>("-1", "用户openId和用户手机号至少传一个");
	// }
	// if (!isValid("couponNum", map)) {
	// return new AccessResponse<String>("-1", "优惠券金额为空");
	// }
	// if (!isValid("activityName", map)) {
	// return new AccessResponse<String>("-1", "活动名称为空");
	// }
	//
	// // 获取mpId
	// Integer mpId = getMpId(appId);
	// if (mpId == 0) {
	// return new AccessResponse<String>("-1", "appId错误");
	// }
	//
	// // 根据mpId查询品牌
	// CrmBrandInfo brandInfo =
	// crmBrandInfoService.selectCrmBrandInfoByMpId(mpId);
	// if (brandInfo == null) {
	// return new AccessResponse<String>("-1", "appId错误");
	// }
	//
	// if (isValid("mobile", map)) { // 如果传递的是用户手机号，根据手机号发送优惠券
	// return custInfoService.sendCouponByMobile(new
	// BigDecimal(map.get("couponNum").toString()),
	// map.get("mobile").toString(), mpId, brandInfo.getBrandId(),
	// map.get("activityName").toString());
	// } else { // 如果传递的是用户openId，根据openId发送优惠券
	// return custInfoService.sendCouponByOpenId(new
	// BigDecimal(map.get("couponNum").toString()),
	// map.get("openId").toString(), mpId, brandInfo.getBrandId(),
	// map.get("activityName").toString());
	// }
	// } catch (Exception e) {
	// LogUtil.error("========发送优惠券异常", e);
	// return new AccessResponse<String>("-1", "系统异常");
	// }
	// }
	//
	@RequestMapping(value = "/sendCoupon/{appId}", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
	public Object sendCoupon(@PathVariable("appId") String appId, @RequestBody Map<String, Object> map) {
		LogUtil.info("=======发送优惠券参数：" + map.toString());

		try {
			// 数据校验
			if (StringUtils.isBlank(appId)) {
				return new AccessResponse<String>("-1", "公众号appId为空");
			}
			if (!isValid("openId", map) && !isValid("mobile", map)) {
				return new AccessResponse<String>("-1", "用户openId和用户手机号至少传一个");
			}
			if (!isValid("couponNum", map)) {
				return new AccessResponse<String>("-1", "优惠券模版Id不能为空");
			}
			if (!isValid("activityName", map)) {
				return new AccessResponse<String>("-1", "活动名称为空");
			}

			// 获取mpId
			Integer mpId = getMpId(appId);
			if (mpId == 0) {
				return new AccessResponse<String>("-1", "appId错误");
			}

			// 根据mpId查询品牌
			CrmBrandInfo brandInfo = crmBrandInfoService.selectCrmBrandInfoByMpId(mpId);
			if (brandInfo == null) {
				return new AccessResponse<String>("-1", "appId错误");
			}
			String mobile = "";
			if (isValid("mobile", map)) {
				mobile = map.get("mobile").toString();
			}
			String openId = "";
			if (isValid("openId", map)) {
				openId = map.get("openId").toString();
			}
			String templateId = map.get("couponNum").toString();
			String changeReason = map.get("activityName").toString();

			return custInfoService.sendCouponToCust(templateId, mobile, openId, mpId, brandInfo.getBrandId(),
					changeReason);

			// if (isValid("mobile", map)) { // 如果传递的是用户手机号，根据手机号发送优惠券
			// return custInfoService.sendCouponByMobile(new
			// BigDecimal(map.get("couponNum").toString()),
			// map.get("mobile").toString(), mpId, brandInfo.getBrandId(),
			// map.get("activityName").toString());
			// } else { // 如果传递的是用户openId，根据openId发送优惠券
			// return custInfoService.sendCouponByOpenId(new
			// BigDecimal(map.get("couponNum").toString()),
			// map.get("openId").toString(), mpId, brandInfo.getBrandId(),
			// map.get("activityName").toString());
			// }
		} catch (Exception e) {
			LogUtil.error("========发送优惠券异常", e);
			return new AccessResponse<String>("-1", "系统异常");
		}
	}

	/**
	 * 封装新建会员对象
	 * 
	 * @param map
	 * @param brandInfo
	 * @return
	 * @author makunsmac
	 */
	private CRMMemberRequest packageCRMMemberRequest(Map<String, Object> map, CrmBrandInfo brandInfo) {
		CRMMemberRequest param = new CRMMemberRequest();
		param.setMobile(map.get("mobile").toString());
		param.setCardNum(map.get("mobile").toString());
		param.setBrandId(brandInfo.getBrandId());
		param.setFullName(map.get("custName").toString());
		param.setGender(map.get("sex").toString());
		param.setBirthday(map.get("birthday").toString());
		if (map.get("hobby") != null) {
			param.setHobby(map.get("hobby").toString());
		}
		return param;
	}

	/**
	 * 通过openId获取头像，昵称，电话
	 * 
	 * @throws Exception
	 */
	@RequestMapping(value = "/getFansInfoByOpenId/{appId}", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String getFansInfoByOpenId(@PathVariable("appId") String appId, @RequestBody Map<String, Object> map)
			throws Exception {
		AccessResponse<Map<String, Object>> access = new AccessResponse<Map<String, Object>>();
		if (map.containsKey("openId")) {
			Integer mpId = this.getMpId(appId);
			Map<String, Object> param = new HashMap<>();
			param.put("openId", map.get("openId"));
			param.put("mpId", mpId);
			Map<String, Object> map2 = wxFansInfoService.selectFanseInfoAndMobileByOpenId(param);
			access.setErrorCode("0");
			access.setErrorMsg("获取用户信息成功");
			access.setData(map2);

		} else {
			access.setErrorCode("1");
			access.setErrorMsg("openId不能为空");
			access.setData(new HashMap<String, Object>());
		}
		return JsonUtils.objectToJson(access);
	}

	@Test
	public void test() throws Exception {
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", "张三");
		map.put("birthday", "2017-10-15");
		Object date = DateUtils.toDate(Date.class, map.get("birthday"));
		map.put("birthday", date);
		CustInfo mapToObject = Tools.toBean(map, CustInfo.class);
		System.out.println(mapToObject);
	}

}
